<?php
namespace app\controller;

use app\service\UserService;
use app\model\UserModel;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;
use think\facade\Cookie;
use think\Request;

class User
{
    protected $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }
    public function verify()
    {
        return Captcha::create();
    }
    public function uploadImg(Request $request)
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        if(!empty($file)){
            try {
                validate(['image'=>'fileSize:10240|fileExt:jpg|image:200,200,jpg'])->check($file);

            }catch (ValidateException $e){
                echo $e->getMessage();
            }
            // 上传到本地服务器
            $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//            $savename = \think\facade\Filesystem::putFileAs( 'topic', $file, 'abc.jpg');//希望用指定的名字来保存文件
        }
        return view('uploadimg');
    }


    public function userList(Request $request)
    {
        try {
            $info = $request->param();
        $data = $this->userService->userList($info);
//            $userModel = new UserModel();
//            $data = $userModel->userDataList($info);
            return view('index',$data);
        }catch (ValidateException $e){
          return  json($e->getError());
        }catch (\Exception $e){
            return json($e->getMessage());
        }

    }
    public function userListByPage(Request $request)
    {
        $info = $request->param();
        $data = $this->userService->userListByPage($info);
        return view('index',$data);
    }
    public function userDetail(Request $request)
    {
        $id = $request->param('id');
//        $data = $this->userService->getUserById($id);//用service的方法调取数据
        /********调用model方法获取数据开始********/
        $userModel = new UserModel();
        $data = $userModel->getUserById($id);
//        Cookie::set('uid', $id, 3600);//设置cookie
        //        Cookie::get('uid');//获取cookie
//        Cookie::delete('uid');//删除cookie
        cookie('name', $data->csc_user, 3600);//助手函数
        cookie('name');//获取cookie
        cookie('name',null);//删除cookie

        /**********调用model方法获取数据结束*****/
        return view('detail', ['data' => $data]);
    }

    public function addUser(Request $request)
    {
        $info = $request->param();
        $userModel = new UserModel();
        $result = $userModel->addUser($info);
        if($result){
            return json(['message' => 'User created successfully'], 201);
        }else{
            return json(['error' => 'User creation failed'], 500);
        }
//        if ($this->userService->addUser($info)) {
//            return json(['message' => 'User created successfully'], 201);
//        } else {
//            return json(['error' => 'User creation failed'], 500);
//        }

    }

    public function editUser(Request $request)
    {
        $info = $request->param();
        $userModel = new UserModel();
        $result = $userModel->updateUser($info);
        if($result){
            return json(['message' => 'User updated successfully'], 201);
        }else{
            return json(['error' => 'User update failed'], 500);
        }
//        if ($this->userService->editUser($info)) {
//            return json(['message' => 'User edited successfully'], 201);
//        } else {
//            return json(['error' => 'User edited failed'], 500);
//        }

    }

    public function deleteUserTwo(Request $request)
    {

        $id = $request->param('id');
        $userModel = new UserModel();
        $result = $userModel->deleteUser($id);
        if ($result) {
            return json(['message' => 'User deleted successfully'], 201);
        } else {
            return json(['error' => 'User deleted failed'], 500);
        }
    }
    public function delUser(Request $request)
    {
        $id = $request->param('id');
        if ($this->userService->deletedUser($id)) {
            return json(['message' => 'User deleted successfully'], 201);
        } else {
            return json(['error' => 'User deleted failed'], 500);
        }

    }
    public function userStatistics(Request $request)
    {
        $info = $request->param();
        $data = $this->userService->getStatistics($info);
        return view('statistic', $data);
    }

}
